package com.caseProject.puremvc.mediator.gui.simple {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextExtent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author Ricardo Teixeira
	 */
	public class SimpleState extends Sprite {
		
		private var view:Sprite;
		private var field:TextField;
		
		private var _text:String = "";
		
		public function SimpleState() {
			this.addEventListener (Event.ADDED_TO_STAGE, onInit);
		}
		
		private function onInit(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, onInit);
			
			this.field = new TextField ();
			this.field.border = false;
			this.field.borderColor = 0xFF0000;
			this.field.selectable = false;
			this.field.multiline = true;
			this.field.autoSize = TextFieldAutoSize.LEFT;
			this.field.type = TextFieldType.DYNAMIC;
			this.field.wordWrap = true;
			
			var format:TextFormat = new TextFormat ();
			format.color = 0x000000;
			format.size = 14;
			format.font = "Arial";
			this.field.defaultTextFormat = format;
			this.field.setTextFormat (format);
			
			this.view = new Sprite ();
			this.addChild (this.view);
			this.addChild (this.field);
			
			this.redraw (100,50);
			
			this.addEventListener (MouseEvent.MOUSE_DOWN, onDown);
		}
		
		private function redraw(w:Number = 100, h:Number = 50):void {
			
			if (w < this.field.width + 25) {
				w = this.field.width + 25;
			}
			if (h < this.field.height + 25) {
				h = this.field.height + 25;
			}
			
			this.view.graphics.clear ();
			this.view.graphics.beginFill (0xFFFFFF, 1);
			this.view.graphics.lineStyle (2, 0x000000, 1, true, "normal");
			this.view.graphics.drawRoundRect (0, 0, w, h, 20, 20);
						
			this.field.x = 15;
			this.field.y = 15;
			this.field.width = this.view.width - 30;
			this.field.height = this.view.height - 30;
		}
		
		private function onDown(e:MouseEvent):void {
			this.stage.addEventListener (MouseEvent.MOUSE_UP, onUp);
			this.addEventListener (MouseEvent.MOUSE_UP, onUp);
				
			if (this.mouseX > this.view.width - 30 && this.mouseY > this.view.height - 30) {				
				this.stage.addEventListener (MouseEvent.MOUSE_MOVE, onMove);				
				this.addEventListener (MouseEvent.MOUSE_MOVE, onMove);
					
			} else {
				this.startDrag ();
			}
		}
		
		private function onMove(e:MouseEvent):void {
			this.redraw (this.mouseX + 5, this.mouseY + 5);
		}
		
		private function onUp(e:MouseEvent):void {
			this.removeEventListener (MouseEvent.MOUSE_UP, onUp);
			this.removeEventListener (MouseEvent.MOUSE_MOVE, onMove);
			
			this.stage.removeEventListener (MouseEvent.MOUSE_UP, onUp);
			this.stage.removeEventListener (MouseEvent.MOUSE_MOVE, onMove);
			trace ("ON_UP")
			
			this.stopDrag ();
		}
		
		private function update():void {
			var extent:TextExtent = TextFieldExtent.getTextExtent (this.text, this.field.defaultTextFormat);
						
			if (extent.textFieldWidth > 100) {
				this.field.width = 100;
				this.field.height = 300;// extent.textFieldHeight;
				this.field.autoSize = TextFieldAutoSize.LEFT;
			} else {
				this.field.width = extent.textFieldWidth;
				this.field.height = extent.textFieldHeight;
			}			
			
			
			
			trace (this.field.width);
			
			this.view.width = this.field.width + 30;
			this.view.height = this.field.height + 30;
			
			this.field.x = 15;
			this.field.y = 15;
		}
		
		public function get text():String {
			return _text;
		}
		
		public function set text(value:String):void {
			_text = value;
			//this.update ();
			this.field.visible = (value != "");
			this.field.text = _text;
			this.redraw ();
		}
		
	}

}